﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.interfaces;
using Procure.Data.Context;

namespace Procure.Web.product
{
    public class productTasks : ITaskReturnUrl
    {
        public int UserId { get; set; }
        public int TypeID { get; set; }
        public int ParentID { get; set; }
        public string Alias { get; set; }
        public string ReturnUrl { get; private set; }

        public bool Save()
        {
            try
            {
                Procure.Data.Context.ProcureDataContext context = DataFactory.GetDataContext();
                Product p = new Product();
                p.Name = Alias;
                p.Description = string.Empty;
                p.Price = 0M;
                p.ShippingBase = 0M;
                p.ShippingCase = 1;
                p.ShippingUnit = 0M;
                p.StockNumber = string.Empty;
                p.Weight = 0M;
                p.ProductTypeId = 1;
                context.Products.InsertOnSubmit(p);
                context.SubmitChanges();

                ReturnUrl = string.Format("Procure/editProduct.aspx?id={0}", p.Id);
            }
            catch (Exception ex)
            {
                ReturnUrl = string.Format("Procure/{0}.aspx", ex.Message);
            }

            return true;
        }

        public bool Delete()
        {
            Procure.Data.Context.ProcureDataContext context = DataFactory.GetDataContext();

            Product product = context.Products.FirstOrDefault(p => p.Id == ParentID);
            if (product != null)
            {
                context.Products.DeleteOnSubmit(product);
                context.SubmitChanges();
                return true;
            }
            else
                return false;
        }

        public productTasks()
        {

        }
    }
}